﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Data;

namespace JiGBot.Classes
{
    public class DataLoader
    {
        #region Variables
        public static DataSet JiGBotScript;
        public static DataTable Healer;
        public static DataTable Waypoints;
        public static DataTable Targeting;
        public static DataTable Looting;
        public static DataTable Hotkeys;
        public static DataTable HUD;
        public static DataGrid HealerDS;
        public static DataGrid WaypointsDS;
        public static DataGrid TargetingDS;
        public static DataGrid LootingDS;
        public static DataGrid HotkeysDS;
        public static DataGrid HUDDS;
        public static List<Control> HealerControls;
        public static List<Control> WaypointsControls;
        public static List<Control> TargetingControls;
        public static List<Control> LootingControls;
        public static List<Control> HotkeysControls;
        public static List<Control> HUDControls;

        #endregion

        public DataLoader()
        {
            #region Initialise Data Tables
            Healer = new DataTable("Healer");
            Healer.Columns.Add("Spell", typeof(string));
            Healer.Columns.Add("MinMP", typeof(int));
            Healer.Columns.Add("MaxMP", typeof(int));
            Healer.Columns.Add("MinHP", typeof(int));
            Healer.Columns.Add("MaxHP", typeof(int));

            Waypoints = new DataTable("Waypoints");
            Waypoints.Columns.Add("Type", typeof(string));
            Waypoints.Columns.Add("X", typeof(int));
            Waypoints.Columns.Add("Y", typeof(int));
            Waypoints.Columns.Add("Z", typeof(int));

            Targeting = new DataTable("Targeting");
            Targeting.Columns.Add("Name", typeof(string));
            Targeting.Columns.Add("Priority", typeof(int));

            Looting = new DataTable("Looting");
            Looting.Columns.Add("Name", typeof(string));
            Looting.Columns.Add("ID", typeof(int));

            Hotkeys = new DataTable("Hotkeys");
            Hotkeys.Columns.Add("Name", typeof(string));
            Hotkeys.Columns.Add("Code", typeof(string));

            HUD = new DataTable("HUD");
            HUD.Columns.Add("Name", typeof(string));
            HUD.Columns.Add("Code", typeof(string));
            #endregion

            #region Initialise Data Views
            HealerDS = new DataGrid();
            HealerDS.Height = 277;
            HealerDS.Width = 428;
            HealerDS.VerticalAlignment = VerticalAlignment.Top;
            HealerDS.Margin = new Thickness(0, 0, 0, 0);
            HealerDS.ItemsSource = Healer.AsDataView();

            WaypointsDS = new DataGrid();
            WaypointsDS.Height = 277;
            WaypointsDS.Width = 428;
            WaypointsDS.VerticalAlignment = VerticalAlignment.Top;
            WaypointsDS.Margin = new Thickness(0, 0, 0, 0);
            WaypointsDS.ItemsSource = Waypoints.AsDataView();

            TargetingDS = new DataGrid();
            TargetingDS.Height = 277;
            TargetingDS.Width = 428;
            TargetingDS.VerticalAlignment = VerticalAlignment.Top;
            TargetingDS.Margin = new Thickness(0, 0, 0, 0);
            TargetingDS.ItemsSource = Targeting.AsDataView();

            LootingDS = new DataGrid();
            LootingDS.Height = 277;
            LootingDS.Width = 428;
            LootingDS.VerticalAlignment = VerticalAlignment.Top;
            LootingDS.Margin = new Thickness(0, 0, 0, 0);
            LootingDS.ItemsSource = Looting.AsDataView();

            HotkeysDS = new DataGrid();
            HotkeysDS.Height = 277;
            HotkeysDS.Width = 428;
            HotkeysDS.VerticalAlignment = VerticalAlignment.Top;
            HotkeysDS.Margin = new Thickness(0, 0, 0, 0);
            HotkeysDS.ItemsSource = Hotkeys.AsDataView();

            HUDDS = new DataGrid();
            HUDDS.Height = 277;
            HUDDS.Width = 428;
            HUDDS.VerticalAlignment = VerticalAlignment.Top;
            HUDDS.Margin = new Thickness(0, 0, 0, 0);
            HUDDS.ItemsSource = HUD.AsDataView();
            #endregion

            #region Healer Controls
            HealerControls = new List<Control>();
            // Enabled checkbox
            CheckBox HealerEnableCheckbox = new CheckBox();
            HealerEnableCheckbox.Content = "Enable";
            HealerEnableCheckbox.Checked += new RoutedEventHandler(HealerCheckBoxEnabled_Checked);

            // Margin of checkbox
            Thickness HealerEnabledCheckboxMargin = HealerEnableCheckbox.Margin;
            HealerEnabledCheckboxMargin.Bottom = 80;

            // Label for slider controlling minimum delay for healer
            Label HealerMinDelayLabel = new Label();
            HealerMinDelayLabel.Content = "Min Delay";

            // Slider for minimum delay for healer
            Slider HealerMinDelaySlider = new Slider();
            HealerMinDelaySlider.Minimum = 500;
            HealerMinDelaySlider.Maximum = 5000;

            // Add controls to healer grid
            HealerControls.Add(HealerEnableCheckbox);
            HealerControls.Add(HealerMinDelayLabel);
            HealerControls.Add(HealerMinDelaySlider);
            
            #endregion

            #region Waypoint Controls

            WaypointsControls = new List<Control>();

            #endregion

            #region Targeting Controls

            TargetingControls = new List<Control>();

            #endregion

            #region Looting Controls

            LootingControls = new List<Control>();

            #endregion

            #region Hotkeys Controls

            HotkeysControls = new List<Control>();

            #endregion

            #region HUD Controls

            HUDControls = new List<Control>();

            #endregion
        }

        #region Healer Callbacks

        public void HealerCheckBoxEnabled_Checked(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Saving / Loading

        public void InitDataGrid()
        {
            JiGBotScript = new DataSet();
            JiGBotScript.Tables.Add(Healer);
            JiGBotScript.Tables.Add(Waypoints);
            JiGBotScript.Tables.Add(Targeting);
            JiGBotScript.Tables.Add(Looting);
            JiGBotScript.Tables.Add(Hotkeys);
            JiGBotScript.Tables.Add(HUD);
        }

        public void UninitDataGrid()
        {
            JiGBotScript.Tables.Clear();
        }

        public void SaveTree(string filename)
        {
            InitDataGrid();
            JiGBotScript.WriteXml(filename);
            UninitDataGrid();
        }

        public void LoadTree()
        {
            NewTree();
            InitDataGrid();
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Script.xml";
            dlg.DefaultExt = ".xml";
            dlg.Filter = "XML Documents (.xml)|*.xml";
            dlg.ShowDialog();
            JiGBotScript.ReadXml(dlg.FileName);
            UninitDataGrid();
        }

        public void NewTree()
        {
            InitDataGrid();
            foreach (DataTable dt in JiGBotScript.Tables)
            {
                dt.Rows.Clear();
            }
            UninitDataGrid();
        }

        #endregion
    }
}
